package com.hobbit.analyze.analyzers
{
	import com.hobbit.analyze.base.LoadItem;
	import com.hobbit.ui.data.UiComponentData;
	import com.hobbit.ui.data.UiLayoutData;
	import com.hobbit.ui.managers.UiManager;
	
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	/**
	 * UI文件解析器 
	 * @author 晓晔
	 * 
	 */
	public class UiAnalyze implements IAnalyze
	{
		
		
		public function UiAnalyze()
		{
		}
		
		public function analyze(byteArray:ByteArray, loadItem:LoadItem, compFn:Function):void
		{
			byteArray.position = 0;
			var resultStr:String = byteArray.readUTFBytes(byteArray.length);
			loadItem.data = byteArray;
			var uiXML:XML = XML(loadItem.data);
			var len:int = uiXML.*.length();
			
			//-----解析
			for (var i:int = 0; i < len; i++)
			{
				var panelXml:XML = uiXML.*[i];
				var panelData:UiLayoutData = new UiLayoutData();
				var attributsXL:XMLList = panelXml.@*;
				var aLen:int = attributsXL.length();
				//获取面板数据
				for (var j:int = 0; j < aLen; j++)
				{
					panelData[attributsXL[j].name().toString()] = attributsXL[j].toString();
				}
				var compXl:XMLList = panelXml.*;
				var cLen:int = compXl.length();
				
				
				//---用于关系表---ps:关系式需写在panel最后
				if(panelXml.priority.length() != 0)
				{
					var priority:XMLList = panelXml.priority;
					panelData.priorityDic = getPriorityList(priority.@value);
					cLen -= 1;
				}
			
				//----
				
				for (var k:int = 0; k < cLen; k++)
				{
					var uiCompData:UiComponentData = new UiComponentData();
					var cData:XML = compXl[k];
					var cAttributes:XMLList = cData.@*;
					var cALen:int = cAttributes.length();
					//获取组件数据
					for (var n:int = 0; n < cALen; n++)
					{
						uiCompData[cAttributes[n].name().toString()] = cAttributes[n].toString();
					}
					panelData.compDic[uiCompData.name] = uiCompData;
				}
				//改用uimanager来存储字典类方便管理。。
				UiManager.getInstance().saveData(panelData.panelName,panelData);
			}
			//----
			if(compFn != null)
				compFn(loadItem);
		}
		
		/**
		 * 建立关系表 
		 * @param $uiLayoutData
		 * @param $priorityStr
		 * 
		 */
		private function getPriorityList($priorityStr:String):Dictionary
		{
			var dic:Dictionary = new Dictionary();
			var group:Array = $priorityStr.split(",");
			for(var i:int = 0;i<group.length;i++ )
			{
				var valueArr:Array = (group[i] as String).split(":");
				dic[valueArr[0]] = valueArr[1];
			}
			return dic;
		}
		
		
		public function getData(key:String, subKey:String):*
		{
			return null;
		}
		
		/**
		 * 考虑到其他因素。。本类不用此方法获取字典 
		 * @return 
		 * 
		 */
		public function getDataList():Dictionary
		{
			return null;
		}
	}
}